package org.flexlancer.tree
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.Tree;
	import mx.core.ClassFactory;
	import mx.core.DragSource;
	import mx.managers.DragManager;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.enums.AppVariables;
	import org.flexlancer.treeitemrenderer.ElementTreeItemRenderer;
	import org.flexlancer.utils.GeneralUtils;
	import org.flexlancer.utils.StringUtils;

	public class ElementsTree extends Tree
	{
		private var templateData:Dictionary = new Dictionary();
		private var xmlData:XML;
		
		public function ElementsTree()
		{
			super();
			this.doubleClickEnabled = true;
			this.dragEnabled = true;
			this.showRoot = true;
			this.labelField = "@name";
			this.itemRenderer = new ClassFactory(ElementTreeItemRenderer);
		}
		
		public function LoadData():void{
			try{
				var ldrElements:URLLoader = new URLLoader();
					
					
				ldrElements.addEventListener(Event.COMPLETE,OnElementsFileLoad);
				ldrElements.addEventListener(IOErrorEvent.IO_ERROR,OnElementsFileError);
					
				if ( !StringUtils.IsNullOrEmpty(AppVariables.URL_GET_ELEMENTS) ){
					var urlv:URLVariables = new URLVariables();
					var urlr:URLRequest = new URLRequest(AppVariables.URL_GET_ELEMENTS);
					
					urlv.CaseFileID = AppVariables.APP_CASE_FILE_ID;
					urlr.method = URLRequestMethod.POST;
					urlr.data = urlv;
					ldrElements.load(urlr);
					GeneralUtils.Show("Loading Elements...");
				}else{
					Alert.show("Elements URL is missing.");
				}

			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		private function OnElementsFileError(evt:IOErrorEvent):void{
			try{
				Alert.show(AppVariables.URL_GET_ELEMENTS + " file not found","Elements file not found.");
				GeneralUtils.Hide();
			}catch(err:Error){
				Logger.LogE(err);
			}
		}		
			
		private function OnElementsFileLoad(evt:Event):void{
			try{
				GeneralUtils.Hide();
				try{
					this.xmlData = XML(evt.target.data);
					//Alert.show(this.xmlData.toXMLString(),"XML Elements Data");
				}catch(err:Error){
					Alert.show(evt.target.data,"Bad Response from GetElements");
					return;
				}
				
				for each ( var singleGroup:XML in this.xmlData.Group ){
					this.templateData[String(singleGroup.@name)] = singleGroup.children()[0].copy();
					delete singleGroup.children()[0];
					for each ( var singleElement:XML in singleGroup.Element ){
						singleElement.@isBranch = "false";
						singleElement.@enabled = "true";
					}
				}
				
				this.dataProvider = this.xmlData.children();
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		override protected function mouseDoubleClickHandler(event:MouseEvent):void{
			try {
				super.mouseDoubleClickHandler(event);
				/*var SelectedItem:Tree;
				SelectedItem = Tree(evt.currentTarget);
				var tmpSelectedItem:XML = XML(SelectedItem.selectedItem);
				if ( tmpSelectedItem.@enabled == "true" ){
					var clink:ElementDetails = PopUpManager.createPopUp(this,ElementDetails,true) as ElementDetails;
					clink.elementName = tmpSelectedItem.@name;
					clink.details = tmpSelectedItem.@details;
					clink.xmldata = tmpSelectedItem;
					PopUpManager.centerPopUp(clink);
					clink.caller = this;
				}*/
			}catch(err:Error) {
				Logger.LogE(err);
			} 
		}
		
        override protected function mouseMoveHandler(event:MouseEvent):void{
            try{
            	super.mouseMoveHandler(event);
                var dragData:XML= XML(this.selectedItem);
                var ds:DragSource = new DragSource();
                ds.addData(dragData, "TreeObject");
                var img:Image = new Image();
                if ( dragData != null ){
                	img.source = AppVariables.URL_GET_THUMBNAIL + "?id=" + dragData.@ThumbnailID;
                }
                img.width = 100;
                img.height = 100;
                if(dragData.@enabled == "true") {
                	
                DragManager.doDrag(this, ds, event,img,-this.mouseX,-this.mouseY); 
            	}
            }catch(err:Error){
            	Logger.LogE(err);
            }
        }		
	}
}